﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using JiveMessenger.Settings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace JiveMessenger.ViewModels
{
    public class ViewModelSettingsPage : ViewModelBase
    {
        public AppSettings AppSettings
        {
            get
            {
                return Settings.AppSettings.AppSettingsInstance;
            }
        }
        public RelayCommand RemoveServerCommand { get; private set; }
        //public RelayCommand OpenAddServerPopupCommand { get; private set; }
        public RelayCommand<string> AddServerCommand { get; private set; }
        public RelayCommand<DependencyPropertyChangedEventArgs> ContentControlDisabledCommand { get; private set; }
        //public RelayCommand<bool> ToggleButtonCheckedCommand { get; private set; }
        public IEnumerable<int> AgeSource { get; private set; }
        public IEnumerable<int> CountSource { get; private set; }


        private bool _isLabelsEnabled;
        public bool IsLabelsEnabled
        {
            get { return _isLabelsEnabled; }
            set
            {
                if (_isLabelsEnabled != value)
                {
                    _isLabelsEnabled = value;
                    RaisePropertyChanged("IsLabelsEnabled");
                }
            }
        }

        public bool StoreMessagesWhileOffline
        {
            get { return AppSettings.AppSettingsInstance.StoreMessagesWhileOfflineSetting; }
            set
            {
                if (AppSettings.AppSettingsInstance.StoreMessagesWhileOfflineSetting != value)
                {
                    //_storeMessagesWhileOffline = value;
                    //IsAgeListPickerEnabled = value;
                    //IsCountListPickerEnabled = value;
                    IsLabelsEnabled = value;
                    AppSettings.AppSettingsInstance.StoreMessagesWhileOfflineSetting = value;
                    RaisePropertyChanged("StoreMessagesWhileOffline");
                }
            }
        }

        private int _selectedPivotItemIndex;
        public int SelectedPivotItemIndex
        {
            get { return _selectedPivotItemIndex; }
            set
            {
                if (_selectedPivotItemIndex != value)
                {
                    _selectedPivotItemIndex = value;
                    IsAddServerPopupOpen = false;
                    RaisePropertyChanged("SelectedPivotItemIndex");
                }
            }
        }

        public double AddServerPopupWidth
        {
            get
            {
                    return Application.Current.Host.Content.ActualWidth-20;
            }
        }

        private bool _isContentControlEnabled;
        public bool IsContentControlEnabled
        {
            get { return _isContentControlEnabled; }
            set
            {
                if (_isContentControlEnabled != value)
                {
                    _isContentControlEnabled = value;
                    RaisePropertyChanged("IsContentControlEnabled");
                    //IsLabelsEnabled = value;
                }
            }
        }


        private bool _isAddServerPopupOpen;
        public bool IsAddServerPopupOpen
        {
            get { return _isAddServerPopupOpen; }
            set
            {
                if (_isAddServerPopupOpen != value)
                {
                    _isAddServerPopupOpen = value;
                    IsContentControlEnabled = !value;
                    RaisePropertyChanged("IsAddServerPopupOpen");
                }
            }
        }


        public ViewModelSettingsPage()
        {
          //  this.AppSettings = App.AppSettings;

            IsContentControlEnabled = true;
            AgeSource = Enumerable.Range(1, 24);
            CountSource = CreateCountSource();
            IsLabelsEnabled = true;
            if (IsInDesignMode)
                return;
            ContentControlDisabledCommand = new RelayCommand<DependencyPropertyChangedEventArgs>(ContentControlDisabledCommandAction);
            RemoveServerCommand = new RelayCommand(RemoveServerCommandAction);
            //OpenAddServerPopupCommand = new RelayCommand(OpenAddServerPopupCommandAction);
            AddServerCommand = new RelayCommand<string>(AddServerCommandAction);
            //ToggleButtonCheckedCommand = new RelayCommand<bool>(ToggleButtonCheckedCommandAction);
        }

        private void ContentControlDisabledCommandAction(DependencyPropertyChangedEventArgs obj)
        {
            bool value = (bool)obj.NewValue;
            IsLabelsEnabled = value;
        }

        private void AddServerCommandAction(string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                List<string> serverListCopy = new List<string>(AppSettings.AppSettingsInstance.ServerListSetting);
                serverListCopy.Add(str);
                AppSettings.AppSettingsInstance.ServerListSetting = serverListCopy;
                AppSettings.AppSettingsInstance.ServerSetting = str;
                //this.ServerPicker.ItemsSource = null;
                //this.ServerPicker.Items.Clear();
                //this.ServerPicker.ItemsSource = AppSettings.ServerListSetting;
                //if (this.ServerPicker.Items.Contains(txtBoxInput.Text))
                //    this.ServerPicker.SelectedItem = txtBoxInput.Text;
                
            }
            IsAddServerPopupOpen = false;
        }

        //private void ToggleButtonCheckedCommandAction(bool obj)
        //{
        //    throw new NotImplementedException();
        //}

        //private void OpenAddServerPopupCommandAction()
        //{
        //    IsAddServerPopupOpen = true;
        //}

        private void RemoveServerCommandAction()
        {
            if (String.IsNullOrEmpty(AppSettings.AppSettingsInstance.ServerSetting))
                return;
            //if (MessageBox.Show("Are you sure?", string.Format("Delete {0}", this.AppSettings.ServerSetting), MessageBoxButton.OKCancel) == MessageBoxResult.OK)

            var msg = new NotificationMessageAction<MessageBoxResult>(this, "QueryServerDeleteDialog", (r) =>
            {
                if (r == MessageBoxResult.OK)
                {
                    List<string> serverListCopy = new List<string>(AppSettings.AppSettingsInstance.ServerListSetting);
                    serverListCopy.Remove(AppSettings.AppSettingsInstance.ServerSetting);
                    AppSettings.AppSettingsInstance.ServerListSetting = serverListCopy;
                    AppSettings.AppSettingsInstance.ServerSetting = serverListCopy.Count > 0 ? serverListCopy[0] : null;
                }
            });

            Messenger.Default.Send(msg);


        }
        private IEnumerable<int> CreateCountSource()
        {
            return new int[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250 };

        }
    }
}
